/*
   
   ▒█████ ██   ██  ▄████▄   ██ ▄█▀       ██████╗ ██╗   ██╗ ██████╗
  ▒██   ▒███  ▓██▒▒██▀ ▀█   ██▄█▒        ██╔══██╗██║   ██║██╔════╝
  ▒█████░▓██  ▒██░▒▓█    ▄ ▓███▄░        ██████╔╝██║   ██║██║  ███╗
  ░██▒  ░▓██  ░██░▒▓▓▄ ▄██▒▓██ █▄        ██╔══██╗██║   ██║██║   ██║
  ░██░   ▒▒██████ ▒ ▓███▀ ░▒██▒ █▄       ██████╔╝╚██████╔╝╚██████╔╝
   ▒ ░   ░▒▓▒ ▒ ▒ ░ ░▒ ▒  ░▒ ▒▒ ▓▒       ╚═════╝  ╚═════╝  ╚═════╝
   ░     ░░▒░ ░ ░   ░  ▒   ░ ░▒ ▒░
   ░ ░    ░░░ ░ ░ ░        ░ ░░ ░
            ░     ░ ░      ░  ░


	@filename: api.js
	@description: 统一封装通用API函数
	@author: HUIB.TOP
	@version：v1.0
	@date：2023/9/24
*/


/* GHAPI 接口封装 */
var GHAPI = {

	/* aardio 后端接口封装 */
	Win: {

		/* 此处用于判断是否导入aardio模块，避免报错 */
		aardio_exist: !(typeof aardio === "undefined"),

		hitCaption: function() {
			/* 窗口 - 移动接口 */
			let aardio_exist = GHAPI.Win.aardio_exist;
			if (aardio_exist == true) {
				aardio.winform.hitCaption();
			}
		},
		hitMax: function() {
			/* 窗口 - 最大化接口 */
			let aardio_exist = GHAPI.Win.aardio_exist;
			if (aardio_exist == true) {
				aardio.winform.hitMax();
			}
		},
		hitMin: function() {
			/* 窗口 - 最小化接口 */
			let aardio_exist = GHAPI.Win.aardio_exist;
			if (aardio_exist == true) {
				aardio.winform.hitMin();
			}
		},
		close: function() {
			/* 窗口 - 关闭窗口接口 */
			let aardio_exist = GHAPI.Win.aardio_exist;
			if (aardio_exist == true) {
				aardio.winform.close();
			}
		},
		openWin: function(id) {
			/* 
				窗口 - 打开子窗口接口
				id | STRING | 子窗口名称
			*/
			let aardio_exist = GHAPI.Win.aardio_exist;
			if (aardio_exist == true) {
				aardio.win_open(id);
			}
		},
		openURL: function(url) {
			/*
				使用默认浏览器打开链接
			*/
			let aardio_exist = GHAPI.Win.aardio_exist;
			if (aardio_exist == true) {
				aardio.open_url(url);
			}
		},


		/*
			写入配置文件（json） 读写操作需要异步
			filePath   | STRING | 配置文件路径
			OBJ        | OBJ    | 配置文件对象
			thrdTable  | BOOLE  | 是否保存到共享表缓存,true为保存到共享表缓存,false为保存到磁盘
			return     | BOOLE  | 写入成功返回true，否则返回false
		*/
		write_Config: async function(filePath = "", OBJ = {}, thrdTable = false) {

			let json = JSON.stringify(OBJ);
			let aardio_exist = GHAPI.Win.aardio_exist;
			let result = false;

			if (aardio_exist == true) {
				result = await aardio.saveFile(filePath, json, thrdTable);
			}

			//console.log(result);
			return result;
		},


		/*
			读取配置文件（json） 读写操作需要异步
			filePath   | STRING | 配置文件路径
			thrdTable  | BOOLE  | 是否从共享表缓存中读取，true为从缓存读取，false为从磁盘读取
			return     | OBJ    | 成功则返回转换后的JSON对象，否则返回null
		*/
		read_Config: async function(filePath = "", thrdTable = false) {
			let aardio_exist = GHAPI.Win.aardio_exist;
			let json = "";

			if (aardio_exist == true) {
				json = await aardio.loadFile(filePath, thrdTable);
				if (json == "" || json == null) {
					return null;
				}

				json = await JSON.parse(json);
			}
			return json;
		},
		
		/*
			设置开机自动运行
			id     | num | 1:关闭开机自动运行，2:注册表模式，3: 计划任务模式
			return | obj | 返回一个对象
		*/
		autoRUN: async function(id = 1) {
			return await aardio.autoRUN(id);
		},
	},



	/* 鼠标键盘拦截集 */
	prevent: {

		// 阻止网页文本被选中
		onselectstart: function() {
			document.onselectstart = function() {
				return false;
			}
		},

		// 拦截键盘
		onkeydown: function() {
			window.onkeydown = window.onkeyup = window.onkeypress = function(event) {
				window.event.returnValue = false;
				return false;
			}
		},
	},




	GH_copy: function(string) {
		/*
			复制到剪切板 | 调用浏览器原生API，线上环境可能会失效
			
			string | STRING | 需要复制到剪切板的内容
		*/

		navigator.clipboard.writeText(string).then(() => {
			if (string.length < 22) {
				layer.msg("成功复制：" + string);
			} else {
				layer.msg("成功复制到剪切板");
			}
		}).catch(() => {
			layer.msg("复制失败");
		});
	},


	/* 
		RGB转CMYK色彩模型接口
		RGB    | OBJ | rgb对象
		return | OBJ | 返回cmyk对象
	*/
	rgb2cmyk: function(RGB) {

		var R = RGB.r,
			G = RGB.g,
			B = RGB.b;
		if ((R == 0) && (G == 0) && (B == 0)) {
			return [0, 0, 0, 1];
		} else {
			let calcR = 1 - (R / 255),
				calcG = 1 - (G / 255),
				calcB = 1 - (B / 255);

			let K = Math.min(calcR, Math.min(calcG, calcB)),
				C = (calcR - K) / (1 - K) * 100,
				M = (calcG - K) / (1 - K) * 100,
				Y = (calcB - K) / (1 - K) * 100;

			C = Math.round(C.toFixed(2));
			M = Math.round(M.toFixed(2));
			Y = Math.round(Y.toFixed(2));
			K = Math.round(K.toFixed(2));

			return ({
				c: C,
				m: M,
				y: Y,
				k: K
			});
		}
	},
	cmyk2rgb: function(CMYK) {
		/* CMYK转RGB色彩模型 */
		let c, m, y, k;
		let r, g, b;

		c = CMYK.c / 100;
		m = CMYK.m / 100;
		y = CMYK.y / 100;
		k = CMYK.k / 100;

		r = 1 - Math.min(1, c * (1 - k) + k);
		g = 1 - Math.min(1, m * (1 - k) + k);
		b = 1 - Math.min(1, y * (1 - k) + k);


		r = Math.round(r * 255);
		g = Math.round(g * 255);
		b = Math.round(b * 255);

		return ({
			r: r,
			g: g,
			b: b
		});
	},


	rgb2hex: function(rgb_str) {
		/*
			RGB转hex字符串
			rgb_str | rgb字符串，如：rgb(255,255,255)
		*/
		var [r, g, b] = rgb_str.match(/\d+/g);
		return '#' + (r << 16 | g << 8 | b).toString(16);
	},



	rgb2hsl: function(rgb_str) { //rgb转hsl
		var [r, g, b] = rgb_str.match(/\d+/g);
		r /= 255, g /= 255, b /= 255;
		var max = Math.max(r, g, b),
			min = Math.min(r, g, b);
		var h, s, l = (max + min) / 2;

		if (max == min) {
			h = s = 0; // achromatic
		} else {
			var d = max - min;
			s = l > 0.5 ? d / (2 - max - min) : d / (max + min);
			switch (max) {
				case r:
					h = (g - b) / d + (g < b ? 6 : 0);
					break;
				case g:
					h = (b - r) / d + 2;
					break;
				case b:
					h = (r - g) / d + 4;
					break;
			}
			h /= 6;
		}

		h = Math.round(h * 100);
		s = Math.round(s * 100);
		l = Math.round(l * 100);
		return "hsl(" + h + ", " + s + ", " + l + ")";
	},


	/* API接口 */
	api: {
		get_popular_colors: function() {
			/* 获取流行色 */
			let url = "res/lib/popular_colors.json",
				json = "";

			$.ajax({
				url: url,
				async: false,
				type: "GET",
				data: {}
			}).done(function(data, status, xhr) {
				json = data;
			});

			return json;
		},



		seach_pantone: function(color_str) {
			/*
				色号查询潘通色
				color_str | String | 色号字符串，如：rgb(255,255,255)，#FFFFFF等
			*/
			var t = color_str.slice(0, 1);
			var t2 = color_str.slice(0, 3);
			var url = "https://www.qtccolor.com/webapi/color/Search";

			if (t == "#") {
				word = color_str;

			} else if (t2 == "rgb") {
				word = GHAPI.rgb2hex(color_str);

			} else if (t2 == "hsv") {


			} else if (t2 == "cmy") {

			}

			if (word == "") {
				return;
			}

			$.ajax({
				url: url,
				async: false,
				type: "POST",
				data: {
					type: "hex",
					word: word,
					brand: "",
					nearbyId: 0,
					img: 0
				},
				success: function(data) {
					json = data;
				}
			});
			return json;
		},



		cmyk2rgb: function(cmyk_str) {
			/*
				cmyk转rgb色
				线上千通网接口
			*/
			var [c, m, y, k] = cmyk_str.match(/\d+/g);
			var url = "https://www.qtccolor.com/tool/cmyk2rgb.aspx";
		}
	}
}